package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.system.dao.ITjsRolesDAO;
import com.tjsinfo.tjsoa.system.vo.TjsRoles;

public class TjsRolesBo implements ITjsRolesBo {
	private ITjsRolesDAO itd;

	public void setTjsRolesDAO(ITjsRolesDAO itd) {
		this.itd = itd;
	}

	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	/**
	 * @获得当前栏目栏
	 * @param userRoleIds
	 *            用户当前所有权限字符串
	 * @return String
	 */
	public String getMenuList(HttpServletRequest request, String userRoleIds) {
		SimpleExpression a = Restrictions.eq("isMenu", "1");
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsRoles.class);
		criteria.add(a);
		criteria.addOrder(Order.desc("orderNo"));

		// 获得当前项目path
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		// 当前项目所有的menu
		List<TjsRoles> rolesList = qbcDao.getCriteria(criteria);
		List<TjsRoles> orderParentMenu=new ArrayList<TjsRoles>();//装有父栏目,并使之具有顺序
		// 所有的父栏目,并从集合中清楚父栏目
		Map<Integer, String> parentMenu = new HashMap<Integer, String>();
		Map<String, String> parentMenuCon = new HashMap<String, String>();
		for (Iterator iterator = rolesList.iterator(); iterator.hasNext();) {
			TjsRoles tjsRoles = (TjsRoles) iterator.next();
			if (tjsRoles.getParentId() == null|| tjsRoles.getParentId().equals("")){
				parentMenu.put(tjsRoles.getId(), tjsRoles.getName());
				parentMenuCon.put(tjsRoles.getName(), tjsRoles.getIconCls());
				orderParentMenu.add(tjsRoles);
				iterator.remove();
			}
		}
		
		
		// 匹配父栏目与子栏目
		Map<String, String> parentMenuMap = new HashMap<String, String>();//栏目map
		Map<Integer, String> parentMenuOrderMap = new HashMap<Integer, String>();//栏目map
		for (TjsRoles tjsRoles : rolesList) {
			if(userRoleIds.contains(tjsRoles.getRoleId())){//如果具有当前栏目权限
				String parentName=parentMenu.get(tjsRoles.getParentId());
				String jsPath=  "<br>&nbsp;&nbsp;&nbsp;<img src=\""
								+ basePath+tjsRoles.getImgUrl()
								+ "\"/><font class=\"f\"><a href=\""
								+ basePath+tjsRoles.getUrl()
								+ "\" target=\""+tjsRoles.getTarget()+"\">"+tjsRoles.getName()+"</a></font>";
				if(!parentMenuMap.containsKey(parentName)){
					parentMenuMap.put(parentName,jsPath);
					parentMenuOrderMap.put(tjsRoles.getParentId(), parentName);
				}else{
					String url=parentMenuMap.get(parentName);
					url=url+jsPath;
					parentMenuMap.put(parentName,url);
				}
			}
		}
		/*
		Entry<Integer, String> parentMenuArray[]= DealString.getSortedHashtableByKey(parentMenuOrderMap);
		List<String> parentValueList=new ArrayList<String>();
		for (int i = 0; i < parentMenuArray.length; i++) {
			parentValueList.add((String)parentMenuArray[i].getValue());
		}
		*/
		//拼装所有父栏目
		StringBuffer sb=new StringBuffer(); 
		for(TjsRoles tjsRole:orderParentMenu){
			/*
			for (String title:parentValueList) {
				String entry=parentMenuMap.get(title);
				sb.append(",{title:'<b>"+title+"</b>',");
				sb.append("html: '"+entry.substring(4, entry.length())+"',");
				sb.append("border:false,autoScroll:true,");
				String con=parentMenuCon.get(title);
				sb.append("iconCls:'"+con+"'}");
			}
			*/
			if(parentMenuOrderMap.containsKey(tjsRole.getId())){
				String entry=parentMenuMap.get(tjsRole.getName());
				sb.append(",{title:'<b>"+tjsRole.getName()+"</b>',");
				sb.append("html: '"+entry.substring(4, entry.length())+"',");
				sb.append("border:false,autoScroll:true,");
				String con=parentMenuCon.get(tjsRole.getName());
				sb.append("iconCls:'"+con+"'}");
			}
		}
		return sb.substring(1, sb.length()).toString();
	}
	
	
	public List<TjsRoles> selectAllRloes(){
		DetachedCriteria criteria =  DetachedCriteria.forClass(TjsRoles.class);
		criteria.add(Restrictions.isNotNull("parentId"));
		return qbcDao.getCriteria(criteria);
	}

	public List<TjsRoles> selectRolesByName(String rolesname){
		DetachedCriteria criteria =  DetachedCriteria.forClass(TjsRoles.class);
		Restrictions.eq("name", rolesname);
		return qbcDao.getCriteria(criteria);
		
	}
	
	/**
	 * @获得当前栏目栏
	 * @param userRoleIds
	 *            用户当前所有公文流转模块权限字符串
	 * @return String
	 */
	public String getsubMenuList(HttpServletRequest request, String userRoleIds,Integer parentid,String defaultname) {
		SimpleExpression a = Restrictions.eq("isMenu", "1");
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsRoles.class);
		criteria.add(a);
		criteria.add(Restrictions.eq("parentId", parentid));
		criteria.addOrder(Order.desc("orderNo"));

		// 获得当前项目path
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		// 当前项目所有的menu
		List<TjsRoles> rolesList = qbcDao.getCriteria(criteria);
		
		StringBuffer sb=new StringBuffer(1024); 
		sb.append("");
		int i=0;
		for (TjsRoles tjsRoles : rolesList) {
			if (tjsRoles.getParentId() != null && !"".equals(tjsRoles.getParentId())){
				if(userRoleIds.contains(tjsRoles.getRoleId())){//如果具有当前栏目权限
					sb.append("<li><a href=\""+ basePath+tjsRoles.getUrl()
							+ "\" target=\""+tjsRoles.getTarget() +"\"");
					if (i==0){
						sb.append(" class=\"active\" >");
						sb.append(tjsRoles.getName()+"<span id = \"topone\" ></span>");
						i++;
					}else{
						sb.append(">"+tjsRoles.getName());
					}
					sb.append("</a></li>");
				}
			}
		}
		return sb.toString();
	}
	
}
